package com.fit.dao.wholesale;

import java.util.ArrayList;
import java.util.List;

import org.apache.tapestry5.ioc.annotations.Inject;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;

import com.fit.entities.User;
import com.fit.entities.Wholesale;

public class WholesaleDAOImpl implements WholesaleDAO {

	@Inject
	private Session session;
	
	public boolean save(Wholesale wholesale) {
		if (wholesale != null) {
			session.save(wholesale);
			return true;
		}
		return false;
	}

	public Wholesale findByName(String name) {
		ArrayList<Wholesale> list = (ArrayList<Wholesale>) session.createCriteria(Wholesale.class).add(Restrictions.eq("name", name)).list();
		if(list.size()==0){
			return null;
		} 
		return list.get(0);
	}
	
	@SuppressWarnings("unchecked")
	public List<Wholesale> findAll() {
		return session.createCriteria(Wholesale.class).list();
	}
	
	
	public boolean update(Wholesale wholesale) {
		if (wholesale != null) {
			session.update(wholesale);
			return true;
		}
		return false;
	}

	public boolean deleteByName(String name) {
		Wholesale w = findByName(name);
		if (w != null) {
			session.delete(w);
			return true;
		}
		return false;
	}
	
//	public boolean checkRegistrations(Vehicle vehicle) {
//		ArrayList<LicencePlate> list = (ArrayList<LicencePlate>) session.createCriteria(LicencePlate.class).add(Restrictions.eq("vehicle", vehicle)).list();
//		if(list.size()!=0){
//			return true;
//		}
//		return false;
//	}

}
